import { _decorator, Animation, Component, Node, rect, Rect, Sprite, SpriteFrame, tween, UITransform, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Dish')
export class Dish extends Component {

    @property(Sprite)
    bottom: Sprite = null!

    @property(Sprite)
    pic: Sprite = null!

    @property(Node)
    cover: Node = null!

    @property(Sprite)
    mask: Sprite = null!

    @property(Node)
    check: Node = null!

    @property([SpriteFrame])
    masks: SpriteFrame[] = []

    @property([SpriteFrame])
    bottoms: SpriteFrame[] = []

    type: number = 1

    index: number = 0

    covered: boolean = true

    floor: number = 0

    touchable: boolean = true

    removed: boolean = false

    merged: boolean = false

    /**
     * 初始化
     * @param type 类型
     * @param index 顺位
     * @param covered 是否遮挡
     * @param floor 是否位于上层
     */
    init(type: number, index: number, covered: boolean = false, floor: number = 0) {
        this.type = type
        this.pic.spriteFrame = this.pic.spriteAtlas.getSpriteFrame(type + '')
        this.index = index
        this.changeCover(covered)
        this.floor = floor
    }

    pop() {
        let pn: Node = this.pic.node
        let scale: Vec3 = pn.getScale()
        let st: Vec3 = scale.clone().multiply(v3(1.25, 1.25, 1.25))
        tween(pn)
            .to(.2, { scale: st }, { easing: 'quadOut' })
            .to(.2, { scale: scale }, { easing: 'quadIn' })
            .start()
    }

    /**
     * 切换点击状态
     * @param touchable 是否可点击
     */
    touchState(touchable: boolean) {
        this.touchable = touchable
        this.mask.node.active = !this.touchable
    }

    /** 
     * 改变盖子状态
     * @param cover 是否盖上
     */
    changeCover(cover: boolean) {
        this.covered = cover
        this.cover.active = this.covered
        this.mask.spriteFrame = this.masks[cover ? 1 : 0]
        this.mask.node.position = v3(0, cover ? 3 : 0, 0)
        //播放动画
    }

    /** 获取绑定盒 */
    getBoundingBox() {
        let n: number = 0.1
        let ut: UITransform = this.node.getComponent(UITransform)
        let bb: Rect = ut.getBoundingBox()
        let nbb: Rect = rect(bb.x + n, bb.y + n, bb.width - n * 2, bb.height - n * 2)
        return nbb
    }

    /** 合成 */
    merge() {
        if (this.merged) {
            return
        }
        this.merged = true
        this.bottom.spriteFrame = this.bottoms[2]
        let pn: Node = this.pic.node
        let ppos: Vec3 = pn.position.add3f(0, 9, 0)
        pn.position = ppos
    }

    /**
     * 是否显示对号
     * @param b 是否和客人任务对应
     */
    checkForTask(b: boolean) {
        this.check.active = b
    }
}


